<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="orderNum" v-if="!propGoodsId">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入商品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基数类型" prop="baseType">
        <el-select v-model="queryParams.baseType" placeholder="请选择基数类型" clearable size="small">
          <el-option v-for="item in baseTypeOptions" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="调价类型" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择调价类型" clearable size="small">
          <el-option label="上调" :value="1" />
          <el-option label="下调" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="typeText">
        <el-input v-model="queryParams.typeText" placeholder="请输入自定义标签" size="small" />
      </el-form-item>
      <el-form-item label="调价额度" prop="modPrice">
        <el-input v-model="queryParams.modPrice" type="number" placeholder="请输入调价额度" size="small" />
      </el-form-item>
      <el-form-item label="调价时间" prop="timeArr">
        <el-date-picker
          v-model="queryParams.timeArr"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="timeArrChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="报价师" prop="offerName">
        <el-select v-model="queryParams.offerName" placeholder="请选择报价师" filterable clearable size="small">
          <el-option
            v-for="dict in offerNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['recovery:modify:add']"
          v-if="!propGoodsId"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['recovery:modify:export']"
          v-if="!propGoodsId"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modifyList" border>
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="商品编码" align="center" prop="orderNum" width="180" />
      <el-table-column label="商品名称" align="center" prop="goodsName" width="180" show-overflow-tooltip  v-if="!propGoodsId" />
      <el-table-column label="基数类型" align="center" prop="baseType">
        <template slot-scope="scope">
          <el-tag v-for="item in baseTypeOptions" v-if="item.value === scope.row.baseType" :type="item.type">{{item.label}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="调价前金额" align="center" prop="prePrice" />
      <el-table-column label="调价额度" align="center" prop="modPrice" />
      <el-table-column label="调价后价格" align="center" prop="sufPrice" />
      <el-table-column label="调价类型" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1">上调</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status === 2">下调</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否成功" align="center" prop="goodsUpdate" v-if="!propGoodsId">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.goodsUpdate === 1">成功</el-tag>
          <el-tag type="danger" v-else>失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="调价人" align="center" prop="createBy" />
      <el-table-column label="调价时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报价师" align="center" prop="offerName" width="100" :formatter="offerNameFormat" v-if="!propGoodsId" />
      <el-table-column label="标签" align="center" prop="typeText" />
      <el-table-column label="调价原因" align="center" prop="remark" width="120" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调价记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品编码" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="调价额度" prop="modPrice">
          <el-input-number v-model="form.modPrice" :min="0" />
        </el-form-item>
        <el-form-item label="调价类型">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">上调</el-radio>
            <el-radio :label="2">下调</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="自定义标签" prop="typeText">
          <el-input v-model="form.typeText" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="调价原因" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入调价原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModify, addModify, exportModify } from "@/api/recovery/modify";

export default {
  name: "Modify",
  components: {
  },
  props: {
    propGoodsId: {type: String}
  },
  watch: {
    propGoodsId(val) {
      this.queryParams.orderNum = val;
      this.getList()
    },
  },
  data() {
    return {
      // 报价师字典
      offerNameOptions: [],
      baseTypeOptions: [
        {label: '成本价格', value: 1, type: 'success'},
        {label: '预售价格', value: 2, type: 'warning'}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调价记录表格数据
      modifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: this.propGoodsId,
        goodsName: null,
        status: null,
        baseType: null,
        typeText: null,
        modPrice: null,
        timeArr: null,
        params: {}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        modPrice: [
          { required: true, message: "调价金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dayu_offer_teach").then(response => {
      this.tranNameOptions = response.data;
      this.offerNameOptions = response.data;
    });
  },
  methods: {
    timeArrChange(e) {
      if(e == null) {
        this.$delete(this.queryParams.params, "startTime");
        this.$delete(this.queryParams.params, "endTime");
      }else {
        this.$set(this.queryParams.params, "startTime", e[0]);
        this.$set(this.queryParams.params, "endTime", e[1]);
      }
    },
    /** 查询调价记录列表 */
    getList() {
      this.loading = true;
      listModify(this.queryParams).then(response => {
        this.modifyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 报价师字典翻译
    offerNameFormat(row) {
      return this.selectDictLabel(this.offerNameOptions, row.offerName);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        modifyId: null,
        orderNum: null,
        goodsName: null,
        modPrice: null,
        status: 1,
        remark: null,
        baseType: 1,
        typeText: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.timeArrChange();
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加调价记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addModify(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有调价记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportModify(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
